package sample.two.client;

/*
 *   Licensed to the Apache Software Foundation (ASF) under one
 *   or more contributor license agreements.  See the NOTICE file
 *   distributed with this work for additional information
 *   regarding copyright ownership.  The ASF licenses this file
 *   to you under the Apache License, Version 2.0 (the
 *   "License"); you may not use this file except in compliance
 *   with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing,
 *   software distributed under the License is distributed on an
 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *   KIND, either express or implied.  See the License for the
 *   specific language governing permissions and limitations
 *   under the License.
 *
 */


import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.RuntimeIoException;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.example.imagine.step1.server.ImageServer;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.SocketConnector;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import com.syt.mina.handler.DispacherHandler;

/**
 * client for the {@link ImageServer}
 *
 * @author The Apache MINA Project (dev@mina.apache.org)
 * @version $Rev: 597940 $, $Date: 2007-11-25 02:00:09 +0100 (Sun, 25 Nov 2007) $
 */
public class SytClient {
    public static final int CONNECT_TIMEOUT = 3000;

    private String host;
    private int port;
    private SocketConnector connector;
    private IoSession session;

    public SytClient(String host, int port) {
        this.host = host;
        this.port = port;
        connector = new NioSocketConnector();
        connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset
                .forName("UTF-8"))));
        connector.setHandler(new DispacherHandler());
    }

    
    public void connect() {
        ConnectFuture connectFuture = connector.connect(new InetSocketAddress(host, port));
        connectFuture.awaitUninterruptibly(CONNECT_TIMEOUT);
        try {
            session = connectFuture.getSession();
        }
        catch (RuntimeIoException e) {
           
        }
    }

    public void disconnect() {
        if (session != null) {
            session.close().awaitUninterruptibly(CONNECT_TIMEOUT);
            session = null;
        }
    }
    
    public boolean isConnected() {
        return (session != null && session.isConnected());
    }
    
    public void say(String str){
    	session.write(str);
    }
    
    
    public static void main(String[] args){
    	SytClient sytClient = new SytClient("127.0.0.1",8880);
    	sytClient.connect();
    	sytClient.say("oooooooooooo");
    	sytClient.disconnect();
    }
 
}
